package baidu.com.matedemo;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.baidu.duer.libcore.adapter.CommonAdapter;
import com.baidu.duer.libcore.adapter.ViewHolder;
import com.baidu.duer.smartmate.out.DuerDevice;
import com.baidu.duer.smartmate.out.DuerSDK;

import java.util.ArrayList;
import java.util.List;

import baidu.com.matedemo.base.BaseActivity;
import baidu.com.matedemo.login.LoginActivity;
import baidu.com.matedemo.network.BLEConfigurationActivity;
import baidu.com.matedemo.network.WifiConfigurationActivity;
import baidu.com.matedemo.pair.OauthType;

/**
 * Created by lihui38 on 2017/9/6.
 */

public class MainActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private CommonAdapter<DuerDevice> commonAdapter = null;
    private Button btOauth;
    private Button btBleConfig;
    private Button btWifiConfig;
    private Button btSinglePage;
    private Button btLandiscovery;
    private ListView listView;
    private List<DuerDevice> dataList = new ArrayList<DuerDevice>();

    @Override
    public View onCreateContentViewCompat(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.sample_activity, viewGroup, false);
    }

    @Override
    public void onContentViewCreated(final View view) {
        super.onContentViewCreated(view);
        setTitle("小度之家SDK" + DuerSDK.getDuerSDKVersion());

        btOauth = (Button) findViewById(R.id.bt_oauth);
        btOauth.setOnClickListener(this);

        btSinglePage = (Button) findViewById(R.id.single_page_btn);
        btSinglePage.setOnClickListener(this);

        btBleConfig = (Button) findViewById(R.id.ble_config);
        btBleConfig.setOnClickListener(this);

        btWifiConfig = (Button) findViewById(R.id.wifi_config);
        btWifiConfig.setOnClickListener(this);

        btLandiscovery = (Button) findViewById(R.id.lan_discovery_btn);
        btLandiscovery.setOnClickListener(this);

        listView = (ListView) view.findViewById(R.id.listview);
        listView.setOnItemClickListener(this);

        commonAdapter = new CommonAdapter<DuerDevice>(this, R.layout.item_function, dataList) {
            @Override
            protected void convert(ViewHolder viewHolder, DuerDevice device, int i) {
                viewHolder.setText(R.id.text_view, device.getDeviceId());
            }
        };

        listView.setAdapter(commonAdapter);
        commonAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onResume() {
        super.onResume();

        final List<DuerDevice> list = DemoApp.getInstance().getDuerDeviceList();
        dataList.clear();
        dataList.addAll(list);
        commonAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        if (v.equals(btOauth)) {
            Intent intent = new Intent();
            intent.setClass(this, LoginActivity.class);
            intent.putExtra(DemoConstant.EXTRA_TITLE, "账号登录");
            startActivity(intent);
        } else if (v.equals(btBleConfig)) {
            choiceOauthTypeDialog(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setClass(MainActivity.this, BLEConfigurationActivity.class);
                    intent.putExtra(DemoConstant.EXTRA_TITLE, "蓝牙设备配网");
                    startActivity(intent);
                }
            });
        } else if (v.equals(btWifiConfig)) {
            choiceOauthTypeDialog(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setClass(MainActivity.this, WifiConfigurationActivity.class);
                    intent.putExtra(DemoConstant.EXTRA_TITLE, "AP设备配网");
                    startActivity(intent);
                }
            });
        } else if (v.equals(btLandiscovery)) {
            choiceOauthTypeDialog(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DemoIntent.intentToLanDiscoveryFragment(MainActivity.this);
                }
            });
        } else if (v.equals(btSinglePage)) {
            DemoIntent.intentToSinglePageFragment(this, "H5页面");
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        DuerDevice duerDevice = (DuerDevice) parent.getItemAtPosition(position);
        DemoApp.getInstance().setCurrentDevice(duerDevice);
        DemoIntent.intentToMainFragment(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void choiceOauthTypeDialog(final View.OnClickListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.dialogStyle);
        builder.setTitle(R.string.oauth_title);
        builder.setItems(R.array.oauthArray, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case 0:
                        DemoApp.getInstance().setOauthType(OauthType.AUTHORIZATION_CODE);
                        break;
                    case 1:
                        DemoApp.getInstance().setOauthType(OauthType.DEVICE_CODE);
                        break;
                    case 2:
                        DemoApp.getInstance().setOauthType(OauthType.PRIVILEGE_OAUTH);
                        break;
                    case 3:
                        DemoApp.getInstance().setOauthType(null);
                        break;
                    default:
                        break;
                }
                if (listener != null) {
                    listener.onClick(null);
                }
            }
        });
        builder.show();
    }

}
